//
//  LGDrawerContentView.m
//  LGSliderDrawer
//
//  Created by apple on 2021/3/24.
//

#import "LGSliderDrawerContentView.h"

@interface LGSliderDrawerContentView ()
///侧滑状态 YES 侧滑中
@property (nonatomic, assign) BOOL isTouch;
@property (nonatomic, assign) CGFloat distance;
@property (nonatomic, assign) BOOL isNeedAlpha;

@end

@implementation LGSliderDrawerContentView


- (void)setBaseContainer:(UIView *)baseContainer {
    [_baseContainer removeFromSuperview];
    _baseContainer = baseContainer;
    [self addSubview:baseContainer];
}

/// 更新侧滑状态
- (void)updateSidelip:(BOOL)isSidelip {
    self.isTouch = isSidelip;
}

/// 侧滑抽屉效果
- (void)showWithPointX:(CGFloat)curPointX {
    self.hidden = NO;
    NSLog(@"curPointX = %f",curPointX);
    
    CGFloat distance = self.baseContainer.frame.size.width;
    CGFloat baseContainer_originX = kScreenWidth - distance;
    CGRect baseContainerRect = self.baseContainer.frame;

    CGFloat alpha = self.configuration.maskAlpha;
    
    if (curPointX >= baseContainer_originX &&baseContainer_originX < kScreenWidth) {
        /// 透明度变化
        alpha = alpha * curPointX/baseContainer_originX;
        baseContainerRect.origin.x = curPointX;
    }
    else if (curPointX < baseContainer_originX) {
        baseContainerRect.origin.x = baseContainer_originX;
    }
    else if (curPointX >= kScreenWidth) {
        alpha = 1;
        baseContainerRect.origin.x = kScreenWidth;
    }
    /// 是否需要展示透明度
    if (self.isNeedAlpha) {
        ///默认以黑色背景alpha渐变
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
    }
    self.baseContainer.frame = baseContainerRect;
}

///显示动画
- (void)show {
    self.hidden = NO;
    [self startAnimation:YES];
}

- (void)startAnimation:(BOOL)isShow {
    CGRect rect = self.baseContainer.frame;
    if (isShow) {
        self.hidden = NO;
        rect.origin.x = kScreenWidth - rect.size.width;
    }else {
        rect.origin.x = kScreenWidth;
    }
    
    /// 动画时间
    CGFloat animationTime = 0.25;
    if (self.configuration) {
        animationTime = (isShow?self.configuration.showAnimDuration:self.configuration.HiddenAnimDuration);
    }
    kWself
    [UIView animateWithDuration:animationTime animations:^{
        wself.baseContainer.frame = rect;
        if (wself.isNeedAlpha) {
            ///需要展示透明度变化
            if (isShow) {
                wself.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:wself.configuration.maskAlpha];
            }else {
                wself.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
            }
        }
    } completion:^(BOOL finished) {
        if (!isShow&&finished) {
            [wself removeFromSuperview];
        }
    }];
}

///隐藏动画
- (void)dismiss {
    if (!self.isTouch) {/// 没有触摸情况下才dismiss
        [self startAnimation:NO];
    }
}

/// 点击关闭页面
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.baseContainer) {
        CGPoint point = [touches.anyObject locationInView:self];
        if (!CGRectContainsPoint(self.baseContainer.frame, point)) {
            [self dismiss];
        }
    }else {
        [self dismiss];
    }
}

///是否需要透明度
- (BOOL)isNeedAlpha {
    return self.configuration.maskAlpha>0;
}

- (void)dealloc {
    NSLog(@"JSRightDrawerContentView dealloc");
}

@end
